package com.wwdx.wechat.handler;

import com.wwdx.service.impl.BaseWxService;
import com.wwdx.utils.ConstantHolder;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.joda.time.LocalDate;

import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author liguoliang
 */
public abstract class AbstractMenuHandler extends AbstractHandler {

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) throws WxErrorException{
        BaseWxService service = (BaseWxService) wxMpService;
        String openId = wxMessage.getFromUser(),eventKey = wxMessage.getEventKey(),clickKey1 = "card_ts",clickKey2 = "huofa",clickKey3 = "tanmei",clickKey4 = "huochu",clickKey5 = "shijie";
        WxMpKefuMessage message = new WxMpKefuMessage();
        WxMpUser wxMpUser = service.getUserService().userInfo(openId);
        String content="";Long bookid = 0L;
        if (clickKey1.equalsIgnoreCase(eventKey)){
            String unionId = wxMpUser.getUnionId(),date = new LocalDate().toString(),
                    key = String.format(ConstantHolder.USER_CARD_SET, unionId),cardKey = String.format(ConstantHolder.USER_CARD_DAY_SET, date),
                    cardNewKey = String.format(ConstantHolder.USER_CARD_NEW_SET, date);
            //用户打卡总数
            setOperations.add(key,date + unionId);
            Boolean member = setOperations.isMember(ConstantHolder.USER_CARD_TOTAL_SET, unionId);
            //活跃
            setOperations.add(cardKey,unionId);
            redisTemplate.expire(cardKey,30 * 60 * 60 , TimeUnit.SECONDS);
            //如果不存在，则为新打卡用户
            if (!member){
                //新增
                setOperations.add(cardNewKey,unionId);
                redisTemplate.expire(cardNewKey,30 * 60 * 60 , TimeUnit.SECONDS);
                //打卡用户总数
                setOperations.add(ConstantHolder.USER_CARD_TOTAL_SET,unionId);
            }
            //打卡次数
            int cardCount = setOperations.size(key).intValue() + 1;
            //听书书籍数
            int bookCount = new HashSet<>(listOperations.range(String.format(ConstantHolder.USER_DURATION_LIST,unionId),0,-1)).size();
            content = "早呀，很开心又能在清晨见到你，这是小蜜陪伴你的第"+cardCount+"个清晨。\n\n" +
                    "每天半小时，让读书成为一种习惯。\n" +
                    "\n" +
                    "<a href =\"%s"+"\">今日上新</a>\n";

            message.setMsgType(WxConsts.KefuMsgType.TEXT);
            message.setToUser(openId);
            message.setContent(String.format(content,wechatUrl_ts+"auth.html"));
            service.getKefuService().sendKefuMessage(message);
            //发送卡片
            wechatPublishService.sendCard(wxMpUser,cardCount,bookCount,service);
            return null;
        }else if (clickKey2.equalsIgnoreCase(eventKey)){
            bookid = 1516959738L;
            content = "听书待领取通知：\n\n" +
                    "您有一本《活法》尚未免费领取。\n\n" +
                    "下方是您的专属海报，邀请好友扫码完成助力任务后，即可免费领书~！\n";
        }else if (clickKey3.equalsIgnoreCase(eventKey)){
            bookid = 1516952797L;
            content = "听书待领取通知：\n\n" +
                    "您有一本《谈美》尚未免费领取。\n\n" +
                    "下方是您的专属海报，邀请好友扫码完成助力任务后，即可免费领书~！\n";
        }else if (clickKey4.equalsIgnoreCase(eventKey)){
            bookid = 1516938129L;
            content = "听书待领取通知：\n\n" +
                    "您有一本《活出最乐观的自己》尚未免费领取。\n\n" +
                    "下方是您的专属海报，邀请好友扫码完成助力任务后，即可免费领书~！\n";
        }else if (clickKey5.equalsIgnoreCase(eventKey)){
            bookid = 1529391371L;
            content = "听书待领取通知：\n\n" +
                    "您有一本《世界上的另一个你》尚未免费领取。\n\n" +
                    "下方是您的专属海报，邀请好友扫码完成助力任务后，即可免费领书~！\n";
        }
        message.setMsgType(WxConsts.KefuMsgType.TEXT);
        message.setToUser(wxMpUser.getOpenId());
        message.setContent(content);
        service.getKefuService().sendKefuMessage(message);
        //发送卡片
        wechatPublishService.sendCard(wxMpUser,service,bookid);
        return null;
    }
}